package santeclair.referentiel.optique.tarification.bean.detailTarif;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

public class DetailTarifBuilder {

    private StatutTarif statutTarif;
    private final StringBuffer message;
    private final TarifBuilder tarifMateriauBuilder;
    private final TarifBuilder tarifSurfaceBuilder;
    private final List<TarifBuilder> tarifTraitementBuilders;
    private final TarifBuilder tarifSupplementCylindreBuilder;
    private boolean isDomTom = false;
    private BigDecimal total = new BigDecimal(0);

    private DetailTarifBuilder() {
        tarifMateriauBuilder = TarifBuilder.newInstance(this);
        tarifSurfaceBuilder = TarifBuilder.newInstance(this);
        tarifSupplementCylindreBuilder = TarifBuilder.newInstance(this);
        tarifTraitementBuilders = new ArrayList<>();
        message = new StringBuffer();
    }

    public TarifBuilder tarifMateriau() {
        return tarifMateriauBuilder;
    }

    public TarifBuilder tarifSurface() {
        return tarifSurfaceBuilder;
    }

    public TarifBuilder tarifSupplementCylindre() {
        return tarifSupplementCylindreBuilder;
    }

    public TarifBuilder addTarifTraitement() {
        TarifBuilder tarifTraitementBuilder = TarifBuilder.newInstance(this);
        tarifTraitementBuilders.add(tarifTraitementBuilder);
        return tarifTraitementBuilder;
    }

    public DetailTarifBuilder isDomTom() {
        isDomTom = true;
        return this;
    }

    public DetailTarifBuilder total(BigDecimal total) {
        this.total = total;
        return this;
    }

    public DetailTarifBuilder message(String message) {
        this.message.append(" - ").append(message).append("\n");
        return this;
    }

    public DetailTarifBuilder statutTarif(StatutTarif statutTarif) {
        this.statutTarif = statutTarif;
        return this;
    }

    public DetailTarif build() {
        StatutTarif statutTarifTraitement = StatutTarif.OK;
        List<Tarif> tarifTraitements = null;
        if (!CollectionUtils.isEmpty(tarifTraitementBuilders)) {
            tarifTraitements = new ArrayList<>(tarifTraitementBuilders.size());
            for (TarifBuilder tarifBuilder : tarifTraitementBuilders) {
                Tarif tarifTraitement = tarifBuilder.build();
                if (StatutTarif.ERROR.equals(tarifTraitement.getStatutTarif())) {
                    statutTarifTraitement = StatutTarif.ERROR;
                }
                tarifTraitements.add(tarifTraitement);
            }
        }

        Tarif tarifMateriau = tarifMateriauBuilder.build();
        Tarif tarifSurface = tarifSurfaceBuilder.build();
        Tarif tarifSupplementCylindre = tarifSupplementCylindreBuilder.build();

        List<StatutTarif> currentStatutTarifs = Arrays.asList(statutTarifTraitement, tarifMateriau.getStatutTarif(), tarifSurface.getStatutTarif(),
                        tarifSupplementCylindre.getStatutTarif());

        if (statutTarif == null && currentStatutTarifs.contains(StatutTarif.ERROR)) {
            statutTarif = StatutTarif.ERROR;
        } else if (statutTarif == null) {
            statutTarif = StatutTarif.OK;
        }
        return new DetailTarif(statutTarif, message.toString(), tarifMateriau, tarifSurface, tarifTraitements, tarifSupplementCylindre, isDomTom, total);
    }

    public static DetailTarifBuilder newInstance() {
        return new DetailTarifBuilder();
    }

}
